package com.ssy.lingxi.report.dao.purchase;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.report.model.bo.tally.statistics.PurchasePlanStatisticsTallyVO;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  采购计划DAO
 * @author ds
 * @date 2021/6/3
 * @version 2.0.0
 */
@Service
public class PurchasePlanDAO {

    @Resource
    @Qualifier("purchaseJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    /**
     * 采购竞价-统计数量
     * [采购竞价]
     * @return
     */
    public List<PurchasePlanStatisticsTallyVO> getStatisticsCount() {
        Map<String,PurchasePlanStatisticsTallyVO> map=null;
        List<PurchasePlanStatisticsTallyVO> planStatisticsCount = getPlanStatisticsCount();
        if(CollectionUtil.isNotEmpty(planStatisticsCount)){
            map = planStatisticsCount.stream().collect(Collectors.toMap(o -> o.getMemberId() + ":" + o.getRoleId(), o -> o));
        }
        List<PurchasePlanStatisticsTallyVO> summaryStatisticsCount = getSummaryStatisticsCount();
        if(CollectionUtil.isNotEmpty(summaryStatisticsCount)){
            if(map==null){map=new HashMap<>();}
            for(PurchasePlanStatisticsTallyVO o :summaryStatisticsCount){
                String key=o.getMemberId() + ":" + o.getRoleId();
                PurchasePlanStatisticsTallyVO onlineBiddingVO = map.get(key);
                if(onlineBiddingVO!=null){
                    onlineBiddingVO.setToBeSummaryCount(onlineBiddingVO.getToBeSummaryCount()+o.getToBeSummaryCount());
                }else{
                    map.put(key,o);
                }
            }
        }
        return map!=null? new ArrayList<>(map.values()) :null;
    }

    /**
     * 待汇总需求计划-统计数量
     * [采购计划]
     * @return
     */
    public List<PurchasePlanStatisticsTallyVO> getPlanStatisticsCount() {
        StringBuilder sql = new StringBuilder();
        sql.append(" select ")
                .append(" member_id memberId,role_id roleId,")
                .append(" sum(case when (inner_status in (1,8,9))then 1 else 0 end) toBeCommitValifyCount,")
                .append(" sum(case when (inner_status = 2 )then 1 else 0 end) toBeValifyStep1Count,")
                .append(" sum(case when (inner_status = 3 )then 1 else 0 end) toBeValifyStep2Count,")
                .append(" sum(case when (inner_status = 4 )then 1 else 0 end) toBeExecuteCount")
                .append(" FROM pu_purchase_plan ")
                .append(" group by member_id,role_id;");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PurchasePlanStatisticsTallyVO.class));
    }

    /**
     * 待汇总需求计划-统计数量
     * [采购计划]
     * @return
     */
    public List<PurchasePlanStatisticsTallyVO> getSummaryStatisticsCount() {
        StringBuilder sql = new StringBuilder();
        sql.append(" select ")
                .append(" member_id memberId,role_id roleId,")
                .append(" sum(case when (inner_status = 5 )then 1 else 0 end) toBeSummaryCount")
                .append(" FROM pu_need_plan ")
                .append(" group by member_id,role_id;");
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PurchasePlanStatisticsTallyVO.class));
    }

}
